<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2018/9/2
 * Time: 上午11:45
 */

namespace api\models;


use yii\base\Model;

class RegistForm extends Model
{

    public $email;

    public $username;

    public $password;

    private $_user;

    public $errors = '';

    public function rules()
    {
        return [
            [['email', 'username', 'password'], 'required'],
        ];
    }


    public function register()
    {
        if ($this->validate()) {
            if (\common\models\User::find()->where(['username' => $this->username])->orWhere(['email' => $this->email])->count() > 0) {
                $this->errors = "用户名或邮箱已注册";

                return false;
            }
            $this->_user                = new \common\models\User();
            $this->_user->email         = $this->email;
            $this->_user->username      = $this->username;
            $this->_user->password_hash = \Yii::$app->security->generatePasswordHash($this->password);
            $this->_user->expire_at     = time() + 3600 * 24 * 7;//过期时间1个小时
            if ($this->_user->save()) {
                return true;
            }

        }

        return false;
    }

    public function getExpired()
    {
        return $this->_user->expire_at;
    }

    public function getUser()
    {
        return $this->_user;
    }

}